export const WebScoket = {
  data() {
    return {
      webSocketUrl: null,
      webValue: null,
      webValue1: null,
      webValue3: null,
      checkCode: null
    }
  },
  methods: {
    // getUrlAddress() {
    //   this.$http.post('/xbtPivas/list?pageNum=1&pageSize=10', { pivasKey: 'websocket' }).then(res => {
    //     res = res.data
    //     if (res.code === 200) {
    //       this.webSocketUrl = 'ws://' + res.data[0].pivasIpurl + ':' + res.data[0].pivasPort
    //       //console.log(this.webSocketUrl)
    //     }
    //   })
    // },
    initWebSocket() {
      this.websock = null
      //判断当前浏览器是否支持WebSocket
      if ('WebSocket' in window) {
        //初始化weosocket(必须)
        this.$http.post('/xbtPivas/list?pageNum=1&pageSize=10', { pivasKey: 'websocket' }).then(res => {
          if (res.code === 200) {
            this.webSocketUrl = 'ws://' + res.rows[0].pivasIpurl + ':' + res.rows[0].pivasPort
            this.websock = new WebSocket(this.webSocketUrl + this.webValue) //新建一个webstock对象
            this.websock.onopen = this.websocketonopen //连接成功建立的回调方法
            this.websock.onmessage = this.websocketonmessage //接收到消息的回调方法
            this.websock.onerror = this.websocketonerror //连接发生错误的回调方法
            this.websock.onclose = this.websocketclose //连接关闭的回调方法
          }
        })
      } else {
        alert('当前浏览器 Not support websocket')
      }
    },
    websocketonopen() {
      console.log('---安全连接建立成功---')
      //websocket连接后发送数据(send发送)
      // let actions = { 你要发送给后台的参数 }; //请根据实际项目需要进行修改
      // this.websocketsend(JSON.stringify(actions));
    },
    websocketonerror() {
      //连接建立失败重连
      this.initWebSocket()
    },
    websocketonmessage(e) {
      //数据接收
      console.log(e.data)
    },
    websocketsend(Data) {
      //数据发送
      this.websock.send(Data)
    },
    onbeforeunload() {
      this.websocket.close()
    },

    websocketclose(e) {
      //关闭
      console.log('断开连接', e)
      // this.websock.close()
    },
    initWebSocket1() {
      this.websock1 = null
      //判断当前浏览器是否支持WebSocket
      if ('WebSocket' in window) {
        //初始化weosocket(必须)
        this.$http.post('/xbtPivas/list?pageNum=1&pageSize=10', { pivasKey: 'websocket' }).then(res => {
          res = res.data
          if (res.code === 200) {
            this.webSocketUrl = 'ws://' + res.data[0].pivasIpurl + ':' + res.data[0].pivasPort
            this.websock1 = new WebSocket(this.webSocketUrl + this.webValue1) //新建一个webstock对象
            this.websock1.onopen = this.websocketonopen1 //连接成功建立的回调方法
            this.websock1.onmessage = this.websocketonmessage1 //接收到消息的回调方法
            this.websock1.onerror = this.websocketonerror1 //连接发生错误的回调方法
            this.websock1.onclose = this.websocketclose1 //连接关闭的回调方法
          }
        })
      } else {
        alert('当前浏览器 Not support websocket1')
      }
    },
    websocketonopen1() {
      console.log('---人员连接建立成功---')
      //websocket连接后发送数据(send发送)
      // let actions = { 你要发送给后台的参数 }; //请根据实际项目需要进行修改
      // this.websocketsend(JSON.stringify(actions));
    },
    websocketonerror1() {
      //连接建立失败重连
      this.initWebSocket1()
    },
    websocketonmessage1(e) {
      //数据接收
      console.log(e.data)
    },
    websocketsend1(Data) {
      //数据发送
      this.websock1.send(Data)
    },
    onbeforeunload1() {
      this.websocket1.close()
    },

    websocketclose1(e) {
      //关闭
      console.log('断开连接', e)
      this.websock1.close()
    },
    initWebSocket2() {
      this.websock2 = null
      //判断当前浏览器是否支持WebSocket
      if ('WebSocket' in window) {
        //初始化weosocket(必须)
        this.$http.post('/xbtPivas/list?pageNum=1&pageSize=10', { pivasKey: 'aimanage' }).then(res => {
          res = res.data
          if (res.code === 200) {
            this.webSocketUrl = 'ws://' + res.data[0].pivasIpurl + ':' + res.data[0].pivasPort + '/ws/' + this.checkCode
            this.websock2 = new WebSocket(this.webSocketUrl) //新建一个webstock对象
            this.websock2.onopen = this.websocketonopen2 //连接成功建立的回调方法
            this.websock2.onmessage = this.websocketonmessage2 //接收到消息的回调方法
            this.websock2.onerror = this.websocketonerror2 //连接发生错误的回调方法
            this.websock2.onclose = this.websocketclose2 //连接关闭的回调方法
          }
        })
      } else {
        alert('当前浏览器 Not support websocket2')
      }
    },
    websocketonopen2() {
      console.log('---AI报警连接建立成功---')
      //websocket连接后发送数据(send发送)
      // let actions = { 你要发送给后台的参数 }; //请根据实际项目需要进行修改
      // this.websocketsend(JSON.stringify(actions));
    },
    websocketonerror2() {
      //连接建立失败重连
      this.initWebSocket2()
    },
    websocketonmessage2(e) {
      //数据接收
      console.log(e.data)
    },
    websocketsend2(Data) {
      //数据发送
      this.websock2.send(Data)
    },
    onbeforeunload2() {
      this.websocket2.close()
    },

    websocketclose2(e) {
      //关闭
      console.log('断开连接', e)
      this.websock2.close()
    },
    initWebSocket3() {
      this.websock3 = null
      //判断当前浏览器是否支持WebSocket
      if ('WebSocket' in window) {
        //初始化weosocket(必须)
        this.$http.post('/xbtPivas/list?pageNum=1&pageSize=10', { pivasKey: 'websocket' }).then(res => {
          res = res.data
          if (res.code === 200) {
            this.webSocketUrl = 'ws://' + res.data[0].pivasIpurl + ':' + res.data[0].pivasPort
            this.websock3 = new WebSocket(this.webSocketUrl + this.webValue3 + this.checkCode) //新建一个webstock对象
            this.websock3.onopen = this.websocketonopen3 //连接成功建立的回调方法
            this.websock3.onmessage = this.websocketonmessage3 //接收到消息的回调方法
            this.websock3.onerror = this.websocketonerror3 //连接发生错误的回调方法
            this.websock3.onclose = this.websocketclose3 //连接关闭的回调方法
          }
        })
      } else {
        alert('当前浏览器 Not support websocket3')
      }
    },
    websocketonopen3() {
      console.log('---公共页面安全连接建立成功---')
      //websocket连接后发送数据(send发送)
      // let actions = { 你要发送给后台的参数 }; //请根据实际项目需要进行修改
      // this.websocketsend(JSON.stringify(actions));
    },
    websocketonerror3() {
      //连接建立失败重连
      this.initWebSocket3()
    },
    websocketonmessage3(e) {
      //数据接收
      console.log(e.data)
    },
    websocketsend3(Data) {
      //数据发送
      this.websock3.send(Data)
    },
    onbeforeunload3() {
      this.websocket3.close()
    },

    websocketclose3(e) {
      //关闭
      console.log('断开连接', e)
      this.websock3.close()
    },
    //随机数
    createCode() {
      let code = ''
      var codeLength = 6
      var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
      for (var i = 0; i < codeLength; i++) {
        var index = Math.floor(Math.random() * 10)
        code += random[index]
      }
      this.checkCode = code
    }
  },
  created() {
    this.createCode()
    // this.getUrlAddress()
  }
}
